var gulp = require('gulp')
var concat = require('gulp-concat')
var spritesmith = require('gulp.spritesmith')
var del = require('del')
var path = require('path')

var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server')
var config = require('./webpack.config.js')
var configDev = require('./webpack.config-dev.js')
const serverConfig = require('./serverConfig.js')

var configDll = require('./webpack.config-dll.js');

gulp.task('buildDll', function() {
  webpack(configDll).run(function() {
    console.log('build DLL finished!')
  })
})

gulp.task('concat', function() {
  return gulp.src(['./src/lib/polyfill.min.js', './src/lib/fetch.min.js',
    './src/lib/weixinapi.min.js', './src/lib/modules/egret/egret.min.js', './src/lib/modules/egret/egret.web.min.js', 
    './src/lib/modules/res/res.min.js', './src/lib/modules/eui/eui.min.js'])
    .pipe(concat('vendor.js'))
    .pipe(gulp.dest(path.resolve(__dirname, 'src/lib')))
})

gulp.task('build', ['copy'], function() {
  webpack(config).run(function() {
    console.log('build finished')
  })
})

gulp.task('buildTest', ['copy'], function() {
  config.plugins.unshift()
  config.plugins.push(new webpack.DefinePlugin({
    env: JSON.stringify('dev')
  }))
  // console.log(config.plugins)
  webpack(config).run(function() {
    console.log('build test finished')
  })
})

gulp.task('dev', ['watch'], function() {
  var compiler = webpack(configDev);
  var server = new WebpackDevServer(compiler, {
    hot: true,
    // enable HMR on the server

    contentBase: path.resolve(__dirname, 'dev'),
    // match the output path 

    publicPath: '/',
    // match the output `publicPath` 

    stats: { colors: true },

    proxy: {},

    port: serverConfig.port

  })
  server.listen(serverConfig.port, serverConfig.host);
})

gulp.task('sprite', ['cleanSprite', 'spriteMajiang'], function () {
  console.log('spriteComplete!')
})

gulp.task('delMajiang', function() {
  return del(['./src/sprite/majiang/*']) 
})

gulp.task('delBull', function() {
  return del(['./src/sprite/bull/*']) 
})

gulp.task('delJinhua', function() {
  return del(['./src/sprite/jinhua/*']) 
})

gulp.task('delPaodekuai', function() {
  return del(['./src/sprite/paodekuai/*']) 
})

gulp.task('spriteMajiang', ['delMajiang'], function () {
  var spriteData = gulp.src('./src/img/scoreMajiang/**/*.png').pipe(spritesmith({
    imgName: 'spriteMajiang.png',
    cssName: 'spriteMajiang.css'
  }))
  return spriteData.pipe(gulp.dest('./src/sprite/majiang'))
})

gulp.task('spriteBull', ['delBull'], function () {
  var spriteData = gulp.src('./src/img/bullShare/**/*.png').pipe(spritesmith({
    imgName: 'spriteBull.png',
    cssName: 'spriteBull.css'
  }))
  return spriteData.pipe(gulp.dest('./src/sprite/bull'))
})

gulp.task('spritePaodekuai', ['delPaodekuai'], function () {
  var spriteData = gulp.src('./src/img/paodekuai/**/*.png').pipe(spritesmith({
    imgName: 'spritePaodekuai.png',
    cssName: 'spritePaodekuai.css'
  }))
  return spriteData.pipe(gulp.dest('./src/sprite/paodekuai'))
})

gulp.task('spriteJinhua', ['delJinhua'], function () {
  var spriteData = gulp.src('./src/img/jinhuaShare/**/*.png').pipe(spritesmith({
    imgName: 'spriteJinhua.png',
    cssName: 'spriteJinhua.css'
  }))
  return spriteData.pipe(gulp.dest('./src/sprite/jinhua'))
})

gulp.task('cleanSprite', function() {
  return del([
    './dist/img/**/*'
  ])
})

gulp.task('cleanAll', function() {
  return del([
    './dist/**/*'
  ])
})

gulp.task('copy', ['cleanAll'], function() {
  gulp.src(['./src/lib/**/*'])
    .pipe(gulp.dest('./dist/lib/'))
  gulp.src(['./src/style/*.css'])
    .pipe(gulp.dest('./dist/style/'))
  // gulp.src(['./src/*.html'])
  //   .pipe(gulp.dest('./dist/'))
  gulp.src(['./src/img/**/*'])
    .pipe(gulp.dest('./dist/img'))
  gulp.src(['./src/imgJson/*.json'])
    .pipe(gulp.dest('./dist/imgJson'))
  return gulp.src(['./resource_Publish/**/*'])
    .pipe(gulp.dest('./dist/resource'))
})

gulp.task('copyJs', function() {
  return gulp.src(['./src/lib/*.js'])
    .pipe(gulp.dest('./dist/lib/'))
})

gulp.task('copyCss', function() {
  return gulp.src(['./src/style/*.css'])
    .pipe(gulp.dest('./dist/style/'))
})

gulp.task('copyHtml', function() {
  return gulp.src(['./src/*.html'])
    .pipe(gulp.dest('./dist/'))
})

gulp.task('copyImage', function() {
  return gulp.src(['./src/img/**/*'])
    .pipe(gulp.dest('./dist/img'))
})

gulp.task('copyJson', function() {
  return gulp.src(['./src/*.json'])
    .pipe(gulp.dest('./dist/'))
})

gulp.task('copyRes', function() {
  return gulp.src(['./src/resource/**/*'])
    .pipe(gulp.dest('./dist/resource'))
})

// gulp.task('copyResPub', function() {
//   return gulp.src(['./src/resource/**/*'])
//     .pipe(gulp.dest('./dist/resource'))
// })

gulp.task('watch', ['watchHtml', 'watchCss'], function() {
  console.log('watched')
})

gulp.task('watchHtml', function() {
  return gulp.watch('./src/*.html', function(event) {
    gulp.src(['./src/*.html']).pipe(gulp.dest('./dev'))
  })
})

gulp.task('watchCss', function() {
  return gulp.watch('./src/style/*.css', function(event) {
    gulp.src(['./src/style/*.css']).pipe(gulp.dest('./dev/style'))
  })
})

gulp.task('cleanLocalServer', function() {
  return del([
    './localServer/**/*'
  ])
})

gulp.task('localServer', ['cleanLocalServer'], function() {
  return gulp.src(['./dist/**/*'])
    .pipe(gulp.dest('./localServer'))
})
